package com.iqiyi.pps.epg.api.server.impl;

import com.alibaba.fastjson.JSONObject;
import com.iqiyi.kiwi.utils.Configuration;
import com.iqiyi.pps.epg.api.entity.CrossData;
import com.iqiyi.pps.epg.api.server.iservice.ApiService;
import com.iqiyi.pps.epg.api.utils.Constants;
import com.iqiyi.pps.epg.api.utils.ServiceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.ws.rs.core.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by karkarrotxu on 2016/9/8.
 */
@Service
public class ApiServiceImpl implements ApiService {
    private static Logger logger = LoggerFactory.getLogger(ApiServiceImpl.class);

    @Context
    protected UriInfo uriInfo;
    @Context
    protected HttpHeaders httpHeaders;

    public static final Map<String, String> KEYS_MAP = new HashMap() {{
        put("boss", "fy6qGaAT5xurJzNf");
        put("lego", "NLxJRrdyFZ9Tawd2");
        put("frontend", "7dpHGKMy4hAh3DmC");
    }};

    /**
     * 最大分页数
     */
    public static final int MAX_PAGESIZE = 50;
    /**
     * 最近预约上下线的开始时间距当前毫秒，只能预约2小时后
     */
    public static final int RESERVATION_INTERVAL_MILLISECONDS = 600000;
    public static final boolean DEV_MODE_FLAG = Boolean.valueOf(Configuration.getString("qipu.entity.devMode"));
    public static final String ERROR_CLIENT_TIME = "客户端时间不正确（应该为准确时间前后五分钟范围内），授权失败";
    public static final String ERROR_EXIST = "出现异常";
    public static final String INTERFACE_ERROR = "接口异常";
    public static final String AUTH_FAIL = "授权失败";
    public static final String PARAMETER_ERROR = "参数错误";
    public static final String NO_EXECUTE_RULE_ERROR = "无可执行规则";
    public static final String EXECUTE_RULE_RUNNING_ERROR = "规则已经在执行中";
    public static final String FORBIDDEN_ADD_RULE_ERROR = "规则执行中不允许添加";

    /**
     * 接口调用统计入DB
     *
     * @param logData
     */
    protected void insertInterfaceLog(Map<String, String> logData) {
        ServiceUtil.insertInterfaceLog(logData);
    }

    protected Map<String, String> formatApiLogMap(String serverIp, String strParam) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("typeId", "1");
        map.put("name", "/" + uriInfo.getPath());
        map.put("serverIp", serverIp);
        map.put("reqTime", String.valueOf(System.currentTimeMillis()));
        map.put("appkey", ServiceUtil.getHeaderParameter("From", httpHeaders));
        map.put("params", strParam);
        return map;
    }

    public Cookie getCookie(String name) {
        Map<String, Cookie> cookies = getCookies();
        if (cookies.isEmpty()) {
            return null;
        }
        for (Map.Entry<String, Cookie> item : cookies.entrySet()) {
            if (item.getKey() == name) {
                return item.getValue();
            }
        }
        return null;
    }

    public Map<String, Cookie> getCookies() {
        return httpHeaders.getCookies();
    }

    protected int checkAuthorization() {
       return ServiceUtil.checkAuthorization(httpHeaders, uriInfo);
    }

    public CrossData getCrossData(){
        CrossData crossData =new CrossData() ;
        crossData.setUriIfo(uriInfo);
        crossData.setHttpHeaders(httpHeaders);
        return crossData;
    }

    protected Map<String, Object> quickToMap(JSONObject original) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (original != null) {
            for (Map.Entry<String, Object> entity : original.entrySet()) {
                map.put(entity.getKey(), "" + entity.getValue());
            }
        }
        return map;
    }

}
